#include "../Include/student_pub.h"
#include "../Include/common_pub.h"
#include "../globals.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>

int add_student(const char* id, const char* name) {
    if (student_count >= MAX_STUDENTS) {
        return FULL;
    }
    
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            return DUPLICATE;
        }
    }
    
    strcpy(students[student_count].id, id);
    strcpy(students[student_count].name, name);
    students[student_count].course_count = 0;
    students[student_count].total_credits = 0.0;
    
    student_count++;
    log_message(LOG_INFO, "添加学生: %s %s", id, name);
    return SUCCESS;
}

Student* find_student(const char* id) {
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            return &students[i];
        }
    }
    return NULL;
}

void display_all_students() {
    printf("\n=== 所有学生信息 ===\n");
    if (student_count == 0) {
        printf("暂无学生信息\n");
        return;
    }
    
    for (int i = 0; i < student_count; i++) {
        printf("学号: %s, 姓名: %s, 已选课程: %d, 总学分: %.1f\n",
               students[i].id, students[i].name, 
               students[i].course_count, students[i].total_credits);
    }
}

int delete_student(const char* id) { 
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            for (int j = i; j < student_count - 1; j++) {
                students[j] = students[j + 1];
            }
            student_count--;
            log_message(LOG_INFO, "删除学生: %s", id);
            return SUCCESS;
        }
    }
    return NOT_FOUND;
}

int update_student(const char* id, const char* new_name) { 
    Student* student = find_student(id);
    if (student == NULL) return NOT_FOUND;
    strcpy(student->name, new_name);
    log_message(LOG_INFO, "更新学生: %s 新姓名: %s", id, new_name);
    return SUCCESS;
}

int student_select_course(const char* student_id, const char* course_id) { 
    Student* student = find_student(student_id);
    if (student == NULL) return NOT_FOUND;
    if (student->course_count >= MAX_COURSES) return FULL;
    
    for (int i = 0; i < student->course_count; i++) {
        if (strcmp(student->selected_courses[i], course_id) == 0) {
            return DUPLICATE;
        }
    }
    
    strcpy(student->selected_courses[student->course_count], course_id);
    student->course_count++;
    log_message(LOG_INFO, "学生 %s 选课: %s", student_id, course_id);
    return SUCCESS;
}

int student_drop_course(const char* student_id, const char* course_id) { 
    Student* student = find_student(student_id);
    if (student == NULL) return NOT_FOUND;
    
    for (int i = 0; i < student->course_count; i++) {
        if (strcmp(student->selected_courses[i], course_id) == 0) {
            for (int j = i; j < student->course_count - 1; j++) {
                strcpy(student->selected_courses[j], student->selected_courses[j + 1]);
            }
            student->course_count--;
            log_message(LOG_INFO, "学生 %s 退课: %s", student_id, course_id);
            return SUCCESS;
        }
    }
    return NOT_FOUND;
}
